<?php


namespace app\community\controller;


use app\admin\controller\Common;
use app\index\controller\Base;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;
use think\Validate;

class Market extends Controller
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform",$platform);
    }

    //积分抽奖配置
    public function prize(){
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];


        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("system_prize")->where($where)->order("integral desc")->limit($start,$limit)->select();
                $count = Db::name("system_prize")->where($where)->order("integral desc")->count("*");

                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        return $this->fetch('prize');
    }


    //添加奖项视图
    public function addPrize(){
        if(empty($_POST['id'])){
            return view("addPrize");
        }else{
            $detail = Db::name("system_prize")->where(['id'=>$_POST['id']])->find();
            $this->assign("detail",$detail);
            return view("editPrize");
        }
    }

    //执行添加、修改奖项
    public function doPrize(){
        $common = new Common();
        try{
            $rule = [
                ["integral", "require", "请填写奖励积分"],
                ["probability", "require", "请填写概率"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }

            $prize = Db::name("system_prize")->count("*");
            if($prize>8){
                throw new Exception("奖项最多不能超过8个");
            }
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            $value['auto'] = Session::get("auto");
            if(empty($value['id'])){
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $id = Db::name("system_prize")->insertGetId($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】设置新的奖项,奖项id【".$id."】";
            }else{
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                $id = $value['id'];
                Db::name("system_prize")->update($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】修改奖项信息,奖项id【".$id."】";
            }

            $log['controller'] = "Market";
            $log['methods'] = "doPrize";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除奖项
    public function delPrize(){
        try{
            $id = $_POST['id'];
            $list = Db::name("system_prize")->where(['id'=>['in',$id]])->select();
            Db::name("system_prize")->where(['id'=>['in',$id]])->delete();

            $common = new Common();

            foreach ($list as $k=>$v){
                $log['remark'] = "管理员【" . Session::get("user_name") . "】删除奖项,项目id【".$v['id']."】";
                $log['controller'] = "Market";
                $log['methods'] = "delPrize";
                $common->addLog($log);
            }

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}